/**
 * 首页构造类
 * Created by 杨浩宇 on 2016-01-06.
 */
define(["require", "jquery", 'public/app','domReady',"template"], function (require, $, app,domReady,artTemplate) {

    function SubjectDemo($container) {
        this._$container = $container;
        this._init();
    }

    SubjectDemo.prototype = {
        //定义当前页面的关键字此关键字必须全库唯一，用于向后台数据库获取对应的渲染模板
        _initKey:function(){
            return "subject_demo";
        },
        _init: function () {
            var self= this;
            domReady(function (){
                var selfx= this;
                var template = app.getTempldate(self._initKey()); //根据页面关键字查询要使用的模板
                var path = app.urlBuild(template.path); //构造模板路径
                self._$container.load(path,{}, function (response, status, xhr) {//向真实页面加载模板内容
                    self._initData(template);
                });
            });
        },
        /**
         * 初始化url地址
         * @private
         */
        _initUrlPath: function (styleId) {
            this.urlPaths = {
                getPageInfo :app.urlBuild("dirms/page/pageInfo/findPageInfoByStyleId/"+styleId) //模型的页面样式ID
            };
        },

        /**
         * 初始化类
         * @private
         */
        _initData: function (template) {
            this._initUrlPath(template.styleId);

            var self = this;

            //此处展示，不光是可以用dataReady，对有异步请求的地方，可以直接使用jQuery.ajax进行查询。
            $.ajax({
                url: this.urlPaths.getPageInfo,
                async:false,
                data:{key:"我是查询参数"},
                type:"get",
                success:function(datas){
                    for(var i = 0; i <datas.length; i ++ ){
                        var queryData = datas[i];
                        if(queryData.infoType === "QUERY"){
                            if(queryData.sen === "LEFT"){
                                self.urlPaths.leftQuery = queryData.url;
                            }

                            if(queryData.sen === "TOP"){
                                self.urlPaths.topQuery = queryData.url;
                            }

                            if(queryData.sen === "CONTENT"){
                                self.urlPaths.contentQuery = queryData.url;
                            }
                        }

                    }

                    //通过DataReady先准备好所有页面需要的数据，然后再对页面控件进行统一的初始化
                    var dataReady = app.getDataReady();
                    dataReady.pushDataSource({//逐个放入所有数据源
                        url: self.urlPaths.leftQuery,
                        data:{"key":"left"},
                        key: 'left'
                    });
                    dataReady.pushDataSource({//逐个放入所有数据源
                        url: self.urlPaths.topQuery,
                        data:{"key":"top"},
                        key: 'top'
                    });
                    dataReady.pushDataSource({//逐个放入所有数据源
                        url: self.urlPaths.contentQuery,
                        data:{"infoType":"PAGE","styleId":template.styleId},
                        key: 'content'
                    });
                    dataReady.once(function (readyData) {//所有数据源都加载后执行一次方法
                        //构造页面的3个部分的查询接口
                        var leftData = readyData.left.data;
                        var topData = readyData.top.data;
                        var contentData = readyData.content.data;
                        var subject_data ={
                            leftList :leftData,
                            topData: topData,
                            content: contentData
                        }
                        var subject_content_left_html = artTemplate('subject_content_left_menu', subject_data);
                        $('#subject_content_left').html(subject_content_left_html);
                        var subject_head_menu_html = artTemplate('subject_head_menu', subject_data);
                        $('#subject_head').html(subject_head_menu_html);
                        var subject_main_content_html = artTemplate('subject_main_content', subject_data);
                        $('#subject_content_main').html(subject_main_content_html);

                    });
                }
            })
        },
        _forward: function(jsModel){ //跳转页面
            var Model = require(jsModel);
            var pages = new Model($("#indexFrame"));
        }
    };

    return SubjectDemo;
});